package com.pan.hisystem.service.impl;

import com.pan.hisystem.mapper.PrescriptionDrugsMapper;
import com.pan.hisystem.pojo.*;
import com.pan.hisystem.mapper.PrescriptionMapper;
import com.pan.hisystem.service.IPrescriptionDrugsService;
import com.pan.hisystem.service.IPrescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pan.hisystem.utils.IdUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qingyang
 * @since 2022-03-18
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements IPrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private IPrescriptionDrugsService prescriptionDrugsService;
    @Autowired
    private PrescriptionDrugsMapper prescriptionDrugsMapper;


    /**
     * 添加处方
     * @return
     */
    @Override
    public RespBean addPrescription(Prescription prescription) {
        if(prescription==null){
            return RespBean.error("处方不能为空");
        }
        prescription.setId(IdUtils.getPrescriptionId());
        prescription.setCreateTime(LocalDateTime.now());
        if(1==prescriptionMapper.insert(prescription)){
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    /**
     * 获取根据处方号获取药品
     * @param preId
     * @return
     */
    @Override
    public RespBean getDrugsByPreId(Integer preId) {
        Prescription prescription=prescriptionMapper.getDrugsByPreId(preId);
        if(prescription!=null){
            return RespBean.success("获取成功!",prescription);
        }
        return RespBean.error("获取失败!");

    }

    /**
     * 获得处方总金额
     * @param preId
     * @return
     */
    @Override
    public RespBean getPrice(Integer preId) {
        Prescription prescription=prescriptionMapper.getDrugsByPreId(preId);
        double num=0;
        if(prescription!=null){
            List<PrescriptionDrugs> prescriptionDrugsList=prescription.getPrescriptionDrugs();
            for (PrescriptionDrugs drug : prescriptionDrugsList) {
                num+=drug.getNum()*drug.getDrug().getPrice();
            }
            return RespBean.success("获取成功!",num);
        }
        return RespBean.error("获取失败!");
    }

    /**
     * 门诊缴费
     * @param prescription
     * @return
     */
    @Override
    @Transactional
    public RespBean updatePrescription(Prescription prescription) {
        prescription.setPayStatus(true);
        prescription.setPayTime(LocalDateTime.now());
        int i=prescriptionMapper.updateById(prescription);
        if(i==1){
            return RespBean.success("缴费成功!");
        }
        return RespBean.error("缴费失败!");
    }

    /**
     * 根据卡号获取患者收费信息记录
     * @param mid
     * @param payStatus
     * @return
     */
    @Override
    public List<Prescription> getMes(String mid, Boolean payStatus) {
        List<Prescription> prescriptionList=prescriptionMapper.getMes(mid,payStatus);
        User user=(User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(!prescriptionList.isEmpty()){
            for (Prescription prescription : prescriptionList) {
                prescription.getUser().setRoles(user.getRoles());
            }
        }
        return prescriptionList;
    }

    /**
     * 退药
     * @param id
     * @return
     */
    @Override
    public RespBean backDrugs(Integer id) {
        Prescription prescription=prescriptionMapper.selectById(id);
        if(!prescription.getPayStatus()){
            return RespBean.error("退药失败!");
        }
        prescription.setBackStatus(true);
        if(prescriptionMapper.updateById(prescription)==1){
            return RespBean.success("退药成功!");
        }
        return RespBean.error("退药成功!");
    }

    /**
     * 根据卡号获取处方
     * @param mid
     * @return
     */
    @Override
    public RespBean getPreByMid(String mid) {
        List<Prescription> prescriptionList=prescriptionMapper.getPreByMid(mid);
        if(!prescriptionList.isEmpty()){
            return RespBean.success("获取成功!",prescriptionList);
        }
        return RespBean.error("获取失败!");
    }

    /**
     * 根据处方获取药品（有处方id和药品名）
     * @param preId
     * @return
     */
    @Override
    public RespBean getDrugs(Integer preId) {
        List<PrescriptionDrugs> prescriptionDrugsList=prescriptionDrugsMapper.getDrugs(preId);
        if(!prescriptionDrugsList.isEmpty()){
            return RespBean.success("获取成功!",prescriptionDrugsList);
        }
        return RespBean.error("获取失败!");
    }

    /**
     * 获取患者划价收费信息
     * @param mid
     * @param payStatus
     * @return
     */
    @Override
    public List<Prescription> getChargeMes(String mid, Boolean payStatus) {
        List<Prescription> prescriptionList=prescriptionMapper.getChargeMes(mid,payStatus);
        User user=(User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(!prescriptionList.isEmpty()){
            for (Prescription prescription : prescriptionList) {
                prescription.getUser().setRoles(user.getRoles());
            }
        }
        return prescriptionList;
    }

}
